package com.moon.icoresso.common.utils;

import lombok.SneakyThrows;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;

/**
 * @ClassName CookieUtils
 * @Description
 * @Author menbbo
 * @Date 2021/2/13 13:06
 * @Version V1.0
 **/

public final class CookieUtils {
    public static void setCookie(final HttpServletResponse response,String name,String value,int maxAge,boolean httpOnly,boolean secure){
        setCookie(response,name,value,"/",maxAge,httpOnly,secure);
    }

    @SneakyThrows
    public static void setCookie(final HttpServletResponse response,String name,String value,String path,int maxAge,boolean httpOnly,boolean secure){
        Cookie cookie = new Cookie(name,null);
        cookie.setPath(path);
        cookie.setHttpOnly(httpOnly);
        cookie.setMaxAge(maxAge);
        cookie.setVersion(1);
        cookie.setValue(URLEncoder.encode(value,"utf-8"));
    }
}